package com.itheima.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itheima.common.R;
import com.itheima.common.ThreadCommon;
import com.itheima.pojo.AddressBook;
import com.itheima.service.AddressBookService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/addressBook")
public class AddressBookController {

    @Autowired
    AddressBookService addressBookService;

    //添加收获地址
    @PostMapping
    public R sava(@RequestBody AddressBook addressBook){
        Long tableLogic = ThreadCommon.getTableLogic();
        addressBook.setUserId(tableLogic);
        //addressBook.setCreateUser(tableLogic);
        //addressBook.setUpdateUser(tableLogic);
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AddressBook::getUserId,tableLogic);
        List<AddressBook> list = addressBookService.list(wrapper);
        //判断用户是否有收货地址 如果没有 则新建的这个地址将被标记为默认地址
        if(list.size() == 0){
            addressBook.setIsDefault(1);
        }
        addressBookService.save(addressBook);
        return R.success("添加成功");
    }

    //设置用户默认地址
    @PutMapping("/default")
    public R mainDefault(@RequestBody AddressBook addressBook){
        Long logic = ThreadCommon.getTableLogic();
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AddressBook::getUserId,logic);
        List<AddressBook> list = addressBookService.list(wrapper);
        for (AddressBook book : list) {
            book.setIsDefault(0);
            addressBookService.updateById(book);
        }
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return R.success("操作成功");
    }

    //根据id查询地址
    @GetMapping("/{id}")
    public R getById(@PathVariable Long id){
        //QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        //wrapper.lambda().eq(AddressBook::getId,id);
        //AddressBook one = addressBookService.getOne(wrapper);
        AddressBook byId = addressBookService.getById(id);
        return R.success(byId);
    }

    //查询默认地址
    @GetMapping("/default")
    public R getDefault(){
        Long logic = ThreadCommon.getTableLogic();
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AddressBook::getUserId,logic);
        AddressBook one = addressBookService.getOne(wrapper);
        return R.success(one);
    }

    //查询用户全部地址
    @GetMapping("/list")
    public R getList(){
        Long tableLogic = ThreadCommon.getTableLogic();
        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AddressBook::getUserId,tableLogic);
        List<AddressBook> list = addressBookService.list(wrapper);
        return R.success(list);
    }

    //删除地址
    @DeleteMapping
    public R deletes(Long[] ids){
        List<Long> longs = Arrays.asList(ids);
        addressBookService.removeByIds(longs);
        return R.success("删除成功");
    }

    //修改地址
    @PutMapping
    public R update(@RequestBody AddressBook addressBook){

        QueryWrapper<AddressBook> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(AddressBook::getId,addressBook.getId());
        AddressBook one = addressBookService.getOne(wrapper);
        one.setConsignee(addressBook.getConsignee());
        one.setPhone(addressBook.getPhone());
        one.setSex(addressBook.getSex());
        one.setDetail(addressBook.getDetail());
        one.setLabel(addressBook.getLabel());
        addressBookService.updateById(one);
        return R.success("修改成功");
    }
}
